package com.yeyks.controller.financial;

import com.google.common.collect.Lists;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.common.appcommon.dto.account.AppAccountDTO;
import com.yeyks.common.appcommon.param.finance.AddWithdrawalParam;
import com.yeyks.common.appcommon.param.finance.QueryWithdrawalOrderParam;
import com.yeyks.common.appcommon.result.CommonPage;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.exception.OftenFiledException;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.finance.FinanceWithdrawalParam;
import com.yeyks.common.result.withdrawalorder.AgentWithdrawalOrderResult;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.commonReference.service.hotel.HotelWithdrawalOrderService;
import com.yeyks.controller.BaseController;
import com.yeyks.hotel.dal.domain.HotelWithdrawalOrder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 财务vip运营商财务模块接口
 * @author: dagny
 * @date: 2019/9/10 0010 10:32
 */
@Api(tags = "AppFinanceVipOperatorController财务vip运营商财务模块接口", description = "财务vip运营商财务模块接口[申请提现，体现记录列表]")
@RestController
@RequestMapping("/vip/operator/finance")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppFinanceVipOperatorController extends BaseController {

    private final HotelWithdrawalOrderService hotelWithdrawalOrderService;

    @ApiOperation(value = "财务vip运营商申请提现", notes = "成功返回true,失败抛出错误信息")
    @PostMapping("/add/withdrawal")
    public CommonResult<Boolean> withdrawal(@Valid AddWithdrawalParam param) {

        AppAccountDTO loginDTO = validToken(param.getToken(), param.getVersion(), param.getPlatform());

        List<Integer> typeLists = Lists.newArrayList(1, 10, 11, 12);
        Arguments.isTrue(!typeLists.contains(loginDTO.getType()), "您无权调用");
        param.setType(HotelWithdrawalOrder.OrderType.VIP.value());

        try {
            if (!hotelWithdrawalOrderService.vipOperatorWithdrawal(MyBeanUtils.convert(loginDTO, AccountInfo.class),
                    MyBeanUtils.convert(param, FinanceWithdrawalParam.class))) {
                return CommonResult.failed("新增提现订单异常");
            }
            return CommonResult.success(true);
        }catch (ServiceException e){
            throw new OftenFiledException(e.getMessage());
        }
    }

    @ApiOperation(value = "提现订单查询", notes = "提现订单查询", response = AgentWithdrawalOrderResult.class)
    @PostMapping("/list")
    public CommonResult<CommonPage<AgentWithdrawalOrderResult>> list(@Valid QueryWithdrawalOrderParam param) {

        AppAccountDTO loginDTO = validToken(param.getToken(), param.getVersion(), param.getPlatform());
        param.setOrderType(HotelWithdrawalOrder.OrderType.VIP.value());
        return hotelWithdrawalOrderService.adminAppVipAgentList(param, MyBeanUtils.convert(loginDTO, AccountInfo.class));
    }

}
